package mall

import (
	"context"
	"github.com/samber/lo"
	"github.com/shopspring/decimal"
	"private-domain-overseas-service/admin/business/api/internal/repository/mall"
	"private-domain-overseas-service/pkg/constant"
	"private-domain-overseas-service/pkg/xerr"
	"private-domain-overseas-service/tools/ent"
	"private-domain-overseas-service/tools/ent/hzmall"
	"private-domain-overseas-service/tools/ent/predicate"
	"time"

	"private-domain-overseas-service/admin/business/api/internal/svc"
	"private-domain-overseas-service/admin/business/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MallGoodsListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 推荐商品列表
func NewMallGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MallGoodsListLogic {
	return &MallGoodsListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MallGoodsListLogic) MallGoodsList(req *types.MallGoodsListRequest) (resp *types.MallGoodsListResponse, err error) {
	goodsDao := mall.NewMallGoodsDao(l.ctx, l.svcCtx)
	mallDao := mall.NewMallDao(l.ctx, l.svcCtx)

	//定义计算值
	baiDecimal := decimal.NewFromInt(100)

	//查询数据
	mallGoods, total, err := goodsDao.MallGoodsConditionsPageAll(req.Current, req.PageSize, nil)
	if err != nil {
		return nil, xerr.ErrCodeMsg(xerr.ServerOperationExceptionError, err.Error())
	}

	//组装返回数据
	list := lo.Map(mallGoods, func(item *ent.HzMallGoods, _ int) *types.MallGoodsList {
		//构建查询条件
		mallConditions := make([]predicate.HzMall, 0)
		mallConditions = append(mallConditions, hzmall.IDEQ(int(item.GoodsMallID)))
		hzMallInfo, _ := mallDao.MallConditionsFirst(mallConditions)

		//到手价
		goodsReceivedPriceFen := decimal.NewFromInt(item.GoodsReceivedPrice)
		//原价
		goodsOriginalPriceFen := decimal.NewFromInt(item.GoodsOriginalPrice)
		//（原价 - 到手价）/原价
		discount := goodsOriginalPriceFen.Sub(goodsReceivedPriceFen).Div(goodsOriginalPriceFen).Mul(baiDecimal).StringFixed(2)

		return &types.MallGoodsList{
			Id:                  int64(item.ID),
			GoodsCreatedAt:      time.Unix(item.GoodsCreatedAt, 0).Format(time.DateTime),
			GoodsMallId:         item.GoodsMallID,
			MallName:            hzMallInfo.MallName,
			GoodsImage:          constant.MallGoodsImage + item.GoodsImage,
			GoodsTitle:          item.GoodsTitle,
			GoodsReceivedPrice:  goodsReceivedPriceFen.Div(baiDecimal).StringFixed(2),
			GoodsOriginalPrice:  goodsOriginalPriceFen.Div(baiDecimal).StringFixed(2),
			Discount:            discount,
			GoodsCommissionRate: decimal.NewFromInt(item.GoodsCommissionRate).Div(baiDecimal).StringFixed(2),
			GoodsCommission:     decimal.NewFromInt(item.GoodsCommission).Div(baiDecimal).StringFixed(2),
			GoodsStatus:         item.GoodsStatus,
			GoodsBuyUrl:         item.GoodsBuyURL,
			GoodsDesc:           item.GoodsDesc,
			CreatedAt:           time.Unix(item.CreatedAt, 0).Format(time.DateTime),
			UpdatedAt:           time.Unix(item.UpdateAt, 0).Format(time.DateTime),
		}
	})

	//返回组装数据
	resp = &types.MallGoodsListResponse{
		Page: &types.PageResponse{
			Total:           total,
			PageSize:        req.PageSize,
			RequestPageSize: req.PageSize,
			Current:         req.Current,
		},
		List: list,
	}
	return resp, nil
}
